package com.mall.web.controller.product;

import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.page.TableDataInfo;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.product.domain.biz.ProProductRecommendedBiz;
import com.mall.product.domain.entity.ProProduct;
import com.mall.product.domain.entity.ProProductRecommended;
import com.mall.product.domain.req.ProProductRecommendedRequest;
import com.mall.product.service.ProProductRecommendedService;
import com.mall.product.service.ProProductService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 商品推荐Controller
 *
 * @author zhonglin
 * @date 2020-05-24
 */
@Controller
@RequestMapping("/product/productRecommended")
public class ProProductRecommendedController
    extends BaseController {

  private String prefix = "product/productRecommended";

  @Autowired
  private ProProductRecommendedService proProductRecommendedService;
  @Autowired
  private ProProductService proProductService;

  @RequiresPermissions("product:productRecommended:view")
  @GetMapping()
  public String productRecommended() {

    return prefix + "/productRecommended";
  }

  /**
   * 查询商品推荐列表
   */
  @RequiresPermissions("product:productRecommended:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(ProProductRecommendedRequest proProductRecommended) {

    startPage();
    List<ProProductRecommendedBiz> list = proProductRecommendedService.selectProProductRecommendedList(proProductRecommended);
    return getDataTable(list);
  }

  /**
   * 导出商品推荐列表
   */
  @RequiresPermissions("product:productRecommended:export")
  @Log(title = "商品推荐", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(ProProductRecommendedRequest proProductRecommended) {

    List<ProProductRecommendedBiz>      list = proProductRecommendedService.selectProProductRecommendedList(proProductRecommended);
    ExcelUtil<ProProductRecommendedBiz> util = new ExcelUtil<ProProductRecommendedBiz>(ProProductRecommendedBiz.class);
    return util.exportExcel(list, "productRecommended");
  }

  /**
   * 新增商品推荐
   */
  @GetMapping("/add")
  public String add(ModelMap mmap) {

    List<ProProduct> products = proProductService.selectProductAll();
    mmap.put("products", products);

    return prefix + "/add";
  }

  /**
   * 新增保存商品推荐
   */
  @RequiresPermissions("product:productRecommended:add")
  @Log(title = "商品推荐", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(ProProductRecommended proProductRecommended) {

    return toAjax(proProductRecommendedService.insertProProductRecommended(proProductRecommended));
  }

  /**
   * 修改商品推荐
   */
  @GetMapping("/edit/{productRecommendedId}")
  public String edit(@PathVariable("productRecommendedId") Long productRecommendedId,
                     ModelMap mmap) {

    ProProductRecommendedBiz proProductRecommended = proProductRecommendedService.selectProProductRecommendedById(productRecommendedId);
    mmap.put("proProductRecommended", proProductRecommended);
    return prefix + "/edit";
  }

  /**
   * 修改保存商品推荐
   */
  @RequiresPermissions("product:productRecommended:edit")
  @Log(title = "商品推荐", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(ProProductRecommended proProductRecommended) {

    return toAjax(proProductRecommendedService.updateProProductRecommended(proProductRecommended));
  }

  /**
   * 删除商品推荐
   */
  @RequiresPermissions("product:productRecommended:remove")
  @Log(title = "商品推荐", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {

    return toAjax(proProductRecommendedService.deleteProProductRecommendedByIds(ids));
  }

}
